/**
 * Copyright (c) @2012-2-21 WKM and/or its affiliates. 
 * Use is subject to license terms.
 * All rights reserved.
 */
package org.wkm.xstream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wkm.util.CharsetUtil;
import org.wkm.util.Configuration;
import org.wkm.util.FileUtil;
import org.wkm.util.Home;

import com.thoughtworks.xstream.XStream;

/**
 * XStreamUtil 转换XML与JavaBean对象
 * @version 1.0 @2012-2-21
 * @author WUKUNMENG
 */
public class XStreamUtil<T> {

	
	private XStreamUtil(){}
	
	private static Logger logger = LoggerFactory.getLogger(XStreamUtil.class);
	
	/**
	 * 将JavaBean写入XML文件
	 * @param obj
	 */
	public static void write(Object obj,String fileName){  
        XStream xstream = new XStream();
        String filePath = null;
        try {
        	File dir = new File(Home.getHomePath(),Configuration.TASK_CONFIG);
        	FileUtil.createFile(dir);
        	filePath = Home.merge(dir.getCanonicalPath(), fileName);
        	FileOutputStream fos = new FileOutputStream(filePath);
        	OutputStreamWriter os  = new OutputStreamWriter(fos,CharsetUtil.UTF_8);  
            xstream.toXML(obj,os); //别忘了关 输出流  
            fos.close();
            os.close();
        } catch (FileNotFoundException e) {  
            e.printStackTrace();
            logger.error("can not find:" + filePath);
            logger.error(e.getMessage());
        } catch (IOException e) {  
            e.printStackTrace();
            logger.error("can not write:" + filePath);
            logger.error(e.getMessage());
        }   
    }
	
	/**
	 * 将JavaBean从XML文件读取出来
	 * @param obj
	 */
	@SuppressWarnings("unchecked")
	public static <T> T read(T t, String fileName){  
		XStream sm = new XStream();
		String filePath = null;
		try {
			File dir = new File(Home.getHomePath(),Configuration.TASK_CONFIG);
			FileUtil.createFile(dir);
			filePath = Home.merge(dir.getCanonicalPath(), fileName);
			File file = new File(filePath);
			if(!file.exists()){
				return null;
			}
			FileInputStream ops = new FileInputStream(filePath);   
			Object obj = sm.fromXML(ops);  
			ops.close();
			if(obj != null){
				return (T)obj;
			}
		} catch (FileNotFoundException e) {     
			e.printStackTrace();
			logger.error("can not find:" + filePath);
            logger.error(e.getMessage());
		} catch (IOException e) {  
            e.printStackTrace();
            logger.error("can not read:" + filePath);
            logger.error(e.getMessage());
        }
		
		return null;
	}
	
	public static void main(String[] args) {
		System.out.println(Home.getHomePath());
		File f = new File("d:/temp");
		System.out.println(f.exists());
		File fs = new File(f,"sss");
		System.out.println(fs.exists());
		
	}
}
